<template>
  <div class="app-container">
    <el-button-group style="display: inline-block; margin-bottom: 15px">
      <el-button type="primary" :plain="plainA" @click="plainDataA"
        >设计清单</el-button
      >
      <el-button type="primary" :plain="plainB" @click="plainDataB"
        >施工清单</el-button
      >
      <el-button type="primary" :plain="plainC" @click="plainDataC"
        >养护清单</el-button
      >
      <el-button type="primary" :plain="plainD" @click="plainDataD"
        >管理清单</el-button
      >
    </el-button-group>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item prop="projectId">
        <el-input
          v-model="queryParams.projectId"
          placeholder="全部分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-date-picker
          v-model="queryParams.startDate"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item prop="riskLevel">
        <el-select
          v-model="queryParams.riskLevel"
          placeholder="全部风险等级"

        >
          <el-option
            v-for="item in dict.type.list_risk_level"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="userId">
        <el-select
          v-model="queryParams.userId"
          placeholder="全部排查责任人"
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="(item, index) in correctorUserIdData"
            :key="index"
            :label="item.nickName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="taskList">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="作业工序" align="center" prop="typeName" />
      <el-table-column label="风险因素" align="center" prop="riskFactor" />
      <el-table-column label="风险等级" align="center" prop="riskLevel" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.list_risk_level" :value="scope.row.riskLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="隐患等级" align="center" prop="dangerLevel" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.list_danger_level" :value="scope.row.dangerLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="检查范围" align="center" prop="regionName" />
      <el-table-column
        label="排查开始时间"
        align="center"
        prop="startDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="排查结束时间"
        align="center"
        prop="endDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="排查频次"
        align="center"
        prop="excludeFrequency"
      >
      <template slot-scope="scope">
        {{scope.row.excludeFrequency}}/{{scope.row.dateType}}
      </template>
      </el-table-column>
      <!-- <el-table-column label="时间类型" align="center" prop="dateType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_date_type" :value="scope.row.dateType"/>
        </template>
      </el-table-column> -->
      <el-table-column label="排查责任人" align="center" prop="userName" />
      <!-- <el-table-column label="是否排查" align="center" prop="isExclude" />
      <el-table-column label="计划id" align="center" prop="planId" />
      <el-table-column label="项目id" align="center" prop="projectId" />
      <el-table-column label="清单类型" align="center" prop="listType" /> -->
      <el-table-column label="已排查" align="center" prop="excludeCount">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="lookButton(1,scope.row.id)"
            >{{ scope.row.excludeCount }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="未施工"
        align="center"
        prop="notConstructionCount"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="lookButton(2,scope.row.id)">{{
            scope.row.notConstructionCount
          }}</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="剩余"
        align="center"
        prop="remark3"
      ></el-table-column> -->
      <el-table-column label="产生隐患" align="center" prop="dangerCount">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="lookButton(3,scope.row.id)">{{
            scope.row.dangerCount
          }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="未销项" align="center" prop="notClearCount">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="lookButton(4,scope.row.id)">{{
            scope.row.notClearCount
          }}</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['plan:task:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['plan:task:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-table v-loading="loading" :data="form">
        <el-table-column label="序号" align="center" prop="id" />
        <el-table-column
          label="检查结果"
          align="center"
          prop="hiddenDangerStatus"
        />
        <el-table-column label="检查人员" align="center" prop="checkPeople" />

        <div v-if="title == '未施工'">
          <el-table-column label="责任区域" align="center" prop="checkPeople" />
          <el-table-column label="未施工原因" align="center" prop="checkPeople" />
        </div>
        <div v-else>
          <el-table-column label="隐患内容/检查内容" align="center" prop="id">
          <template slot-scope="scope">
            <el-tag type="warning" effect="dark">{{
              scope.row.hiddenLevel
            }}</el-tag
            >{{ scope.row.hiddenMessage }} {{ scope.row.dutyArea }}
          </template>
        </el-table-column>
        <el-table-column
          label="现场照片"
          align="center"
          prop="attachList"
          width="100"
        >
          <template slot-scope="scope">
            <image-preview
              :src="
                baseUrl +
                '/' +
                scope.row.attachList[0].bucket +
                '/' +
                scope.row.attachList[0].name +
                ''
              "
              :width="50"
              :height="50"
            />
          </template>
        </el-table-column>
        </div>
        
        

        <el-table-column label="检查时间" align="center" prop="checkTime" />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="lookRecord(scope.row)"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">关 闭</el-button>
      </div>

      <el-dialog
        width="45%"
        title="隐患详情"
        :visible.sync="innerVisible"
        append-to-body
      >
        <el-tabs v-model="firstData">
          <el-tab-pane label="用户管理" name="first">
            <el-row>
              <!-- <el-col :span="12">项目名称：{{lookRecordDataList.bizProjectCheck.checkTaskName}}</el-col>
              <el-col :span="12">隐患类别：{{lookRecordDataList.bizProjectCheckRecord.riskLevel}}</el-col>
              <el-col :span="12">隐患明细：{{lookRecordDataList.bizProjectCheckRecord.hiddenMessage}}</el-col>
              <el-col :span="12">补充说明：{{lookRecordDataList.bizProjectCheckRecord.supplement}}</el-col>
              <el-col :span="12">检查类型：{{lookRecordDataList.bizProjectCheckRecord.checkType}}</el-col>
              <el-col :span="12">隐患级别：{{lookRecordDataList.bizProjectCheckRecord.hiddenLevel}}</el-col>
              <el-col :span="12">分包单位：{{lookRecordDataList.bizProjectCheckRecord.subpackageUnit}}</el-col>
              <el-col :span="12">事故隐患：{{lookRecordDataList.bizProjectCheckRecord.accidentHidden}}</el-col>
              <el-col :span="12">整改人：{{lookRecordDataList.bizProjectCheckRecord.revisePeople}}</el-col>
              <el-col :span="12">通知人：{{lookRecordDataList.bizProjectCheckRecord.notifier}}</el-col>
              <el-col :span="12">整改时限：{{lookRecordDataList.bizProjectCheckRecord.reviseTheDates}}</el-col>
              <el-col :span="12">整改要求：{{lookRecordDataList.bizProjectCheckRecord.reviseRequire}}</el-col>
              <el-col :span="24">相关照片：
                <image-upload v-model="lookRecordDataList.bizProjectCheckRecord.photo" />
              </el-col> -->
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
//已排查 1234
//未施工 5
//产生隐患 123
//未销项 12
import {
  listTask,
  getTask,
  delTask,
  addTask,
  updateTask,
  uplookButton,
} from "@/api/biz/exclude/task";
import { listUser } from "@/api/common/user";
import { mapState } from "vuex";
export default {
  name: "Task",
  dicts: ["biz_date_type","list_risk_level","list_danger_level"],
  data() {
    return {
      firstData: "first",
      innerVisible: false,
      baseUrl: process.env.VUE_APP_MINIO_BASE_API,
      plainA: false,
      plainB: true,
      plainC: true,
      plainD: true,
      correctorUserIdData: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 自动生成任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 更新时间时间范围
      daterangeStartDate: [],
      // 更新时间时间范围
      daterangeEndDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: this.$store.state.app.selectData,
        listType: "1",
        riskLevel: null,
        startDate: null,
        endDate: null,
        dateType: null,
        userName: null,
        isExclude: null,
        remark2: null,
        remark3: null,
        remark4: null,
        remark5: null,
        status: null,
        delFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeName: [
          { required: true, message: "作业工序不能为空", trigger: "blur" },
        ],
        riskFactor: [
          { required: true, message: "风险因素不能为空", trigger: "blur" },
        ],
        riskLevel: [
          { required: true, message: "风险等级不能为空", trigger: "blur" },
        ],
        dangerLevel: [
          { required: true, message: "隐患等级不能为空", trigger: "blur" },
        ],
        regionName: [
          { required: true, message: "区域名称不能为空", trigger: "blur" },
        ],
      },
      pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
    };
  },
  created() {
    this.getList();
  },
  computed: {
    ...mapState(["app"]),
  },
  watch: {
    app: {
      handler(newVal, oldVal) {
        this.queryParams.projectId = newVal.selectData;
        this.getList();
      },
      deep: true,
    },
  },
  methods: {
    /** 查询自动生成任务列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeStartDate && "" != this.daterangeStartDate) {
        this.queryParams.params["beginStartDate"] = this.daterangeStartDate[0];
        this.queryParams.params["endStartDate"] = this.daterangeStartDate[1];
      }
      if (null != this.daterangeEndDate && "" != this.daterangeEndDate) {
        this.queryParams.params["beginEndDate"] = this.daterangeEndDate[0];
        this.queryParams.params["endEndDate"] = this.daterangeEndDate[1];
      }
      listTask(this.queryParams).then((response) => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listUser(this.queryParams).then((response) => {
        this.correctorUserIdData = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        typeName: null,
        riskFactor: null,
        riskLevel: null,
        dangerLevel: null,
        regionName: null,
        startDate: null,
        endDate: null,
        excludeFrequency: null,
        dateType: null,
        userName: null,
        isExclude: null,
        planId: null,
        projectId: null,
        listType: null,
        remark: null,
        remark2: null,
        remark3: null,
        remark4: null,
        remark5: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeStartDate = [];
      this.daterangeEndDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加自动生成任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTask(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改自动生成任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTask(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除自动生成任务编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTask(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "plan/task/export",
        {
          ...this.queryParams,
        },
        `task_${new Date().getTime()}.xlsx`
      );
    },
    plainDataA() {
      this.queryParams.listType = "1";
      this.plainA = false;
      this.plainB = true;
      this.plainC = true;
      this.plainD = true;
      this.getList();
    },
    plainDataB() {
      this.queryParams.listType = "2";
      this.plainA = true;
      this.plainB = false;
      this.plainC = true;
      this.plainD = true;
      this.getList();
    },
    plainDataC() {
      this.queryParams.listType = "3";
      this.plainA = true;
      this.plainB = true;
      this.plainC = false;
      this.plainD = true;
      this.getList();
    },
    plainDataD() {
      this.queryParams.listType = "4";
      this.plainA = true;
      this.plainB = true;
      this.plainC = true;
      this.plainD = false;
      this.getList();
    },
    // 已排查
    lookButton(type,id) {
      let data = {
        planId: id,
        projectId: this.queryParams.projectId,
        hiddenDangerStatus: null,
      };
      if (type == 1) {
        data.hiddenDangerStatus = "1,2,3,4"
        this.title ="已排查"
      }else if (type == 2) {
        data.hiddenDangerStatus = "5"
        this.title ="未施工"
      }else if (type == 3) {
        data.hiddenDangerStatus = "1,2,3"
        this.title ="产生隐患"
      }else if (type == 4) {
        data.hiddenDangerStatus = "1,2"
        this.title ="未销项"
      }
      uplookButton(data).then((data) => {
        this.form = data.data;
        this.open = true;
      });
    },
    //检查记录查看        隐患接口
    lookRecord(row) {
      this.innerVisible = true;
    },
  },
};
</script>
